#include<bits/stdc++.h>
using namespace std;

map<string, int>mp;
int n, m, k;
string s[10010];
string y[30];

void dfs(int len, vector<string>v) {
	if(len == k + 1) {
		
	}
	v.push_back(y[len]);
	dfs(len + 1, v);
	v.pop_back();
	dfs(len + 1, v); 
}

int main() {
	
	cin >> n >> m;
	for(int i = 1; i <= n; i ++ ) {
		cin >> s[i];
	}
	
	cin >> k;
	for(int i = 1; i <= k; i ++ ) {
		cin >> y[i];
	}
	
	vector<string>v;
	dfs(1, v);
	
	for(int i = 1; i <= n; i ++ ) {
		cout << (mp[s[i]] == 0 ? -1 : mp[s[i]]) << endl;
	}
	return 0;
}

